#include "Game.hpp"

#include "GameState.hpp"
#include "InMenuState.hpp"
#include "InGameState.hpp"
#include "ResourceManager.hpp"


Game::Game()
    : window_(nullptr)
{
    window_ = new sf::RenderWindow(sf::VideoMode(805, 585),
                                   "Defend Your Desktop",
                                   sf::Style::Close | sf::Style::Titlebar);

    states_.push(InMenuState::instance(this, window_));
}

Game::~Game()
{
    InMenuState::destroy();
    InGameState::destroy();

    delete window_;
    window_ = nullptr;

    ResourceManager<sf::Sprite>::destroy();
    ResourceManager<sf::Texture>::destroy();
}

void
Game::init()
{
    InMenuState::instance(this, window_)->init();
    InGameState::instance(this, window_)->init();
}

void
Game::clean()
{
    while (states_.empty() == false)
        states_.pop();
}

void
Game::handleEvents()
{
    states_.top()->handleEvents();
}

void
Game::update()
{
    states_.top()->update();
}

void
Game::render()
{
    states_.top()->render();
}

bool
Game::isRunning() const
{
    return window_->isOpen();
}

void
Game::changeState(GameState* newState)
{
    states_.top()->reset();
    states_.pop();

    states_.push(newState);
}

void
Game::addState(GameState* newState)
{
    states_.push(newState);
    newState->init();
}
